#!/bin/sh
#**************************************************************************
#*                                                                        *
#*                                 OCaml                                  *
#*                                                                        *
#*                   Sebastien Hinderer, Tarides, Paris                   *
#*                                                                        *
#*   Copyright 2023 Institut National de Recherche en Informatique et     *
#*     en Automatique.                                                    *
#*                                                                        *
#*   All rights reserved.  This file is distributed under the terms of    *
#*   the GNU Lesser General Public License version 2.1, with the          *
#*   special exception on linking described in the file LICENSE.          *
#*                                                                        *
#**************************************************************************

# Bump magic numbers of the OCaml compiler
# The script takes only one argument, the version part of the magic numbers
# This version is a 3-digits string, e.g. 034
# The script makes sure all the necessary files are updated and fails
# if a file that it needs to update does not exist, leaving the repository
# in a partially updated (likely inconsistent) state.

# Also, the script updates both build-aux/ocaml_version.m4 (whose update
# is intended to be committed) and a few other *generated* files which are
# ignored by git but which need to be updated here so that the script
# can be used as part of the bootstrap procedure.
# So the update of build-aux/ocaml_version.m4 (as well as the one this
# implies to configure) are not strictly speaking needed for the
# on-going bootstrap, but rather for future builds. The update of
# generated files, in contrast, *is* required by the on-going bootstrap.

# Fail on error
set -e

if [ ${#1} != 3 ]; then
  echo 'Version number must be 3 bytes' +  exit 2
fi

new_num=$1

# Bump magic numbers in runtime/caml/exec.h

sed -e s/'define MAGIC_NUMBER_VERSION "..."'/"define MAGIC_NUMBER_VERSION \"$new_num\""/ \
  runtime/caml/exec.h > runtime/caml/exec.h.tmp
mv runtime/caml/exec.h.tmp runtime/caml/exec.h

# Bump magic numbers in utils/config.common.ml

sed -e s/'...|magic}'/"$new_num|magic}"/ \
  utils/config.common.ml > utils/config.common.ml.tmp
mv utils/config.common.ml.tmp utils/config.common.ml

# Bump magic numbers in otherlibs/dynlink/dynlink_config.ml

sed -e s/'...|magic}'/"$new_num|magic}"/ \
  otherlibs/dynlink/dynlink_config.ml > otherlibs/dynlink/dynlink_config.ml.tmp
mv otherlibs/dynlink/dynlink_config.ml.tmp otherlibs/dynlink/dynlink_config.ml

# Bump magic numbers in build-aux/ocaml_version.m4

sed -e s/'m4_define(\[MAGIC_NUMBER__VERSION\], \[...\])'/"m4_define([MAGIC_NUMBER__VERSION], [$new_num])"/ \
  build-aux/ocaml_version.m4 > build-aux/ocaml_version.m4.tmp
mv build-aux/ocaml_version.m4.tmp build-aux/ocaml_version.m4

# Regenerate the configure script

tools/autogen
